<?php
class Pagination_Offset
{
    public $record_per_page=5;
    public $current_page=1;
    public $page_range=0;
    public $record_total=0;
    
    /**
     * Get limit and length 
    **/
    public function getOffsets(){
        return array("offset"=>$this->getFirstItemInPage(),"limit"=>$this->record_per_page);
    }

    /**
     * Get All Pages in Array
    **/
    private function getAllPages(){
        return range(1,intval(ceil($this->record_total / $this->record_per_page)));
    }
    
    /**
     * Get Pages in Array
    **/
    public function pages(){
        if($this->record_total > 0){
            //with range 
            if($this->page_range>0){
                $pages = array();
                for($p=$this->current_page; $p>=($this->current_page-$this->page_range);$p--){
                    if($p>0){
                        $pages[] = $p;
                    }
                }
 
                for($n=$this->current_page+1; $n<=($this->current_page+$this->page_range);$n++){
                    if($n<=end($this->getAllPages())){
                       $pages[] = $n; 
                    }
                }
                sort($pages);
                return $pages;
            }
            else{
                //without range 
                return $this->getAllPages();
            }
        }
        return false; 
    }
    
    /**
     * Get Previous page
    **/
    public function prev(){
        $prev = $this->current_page - 1;
        if($prev>0){
            return $prev;
        }
        return false;
    }
    
    /**
     * Get Next page
    **/
    public function next(){
        $next = $this->current_page + 1;
        if($next<=end($this->getAllPages())){
            return $next;
        }
        return false;
    }
    
    /**
     * Get First page
    **/
    public function first(){
        return intval(1);
    }
    
    /**
     * Get Last page
    **/
    public function last(){
        return end($this->getAllPages());
    }
    
    /**
     * Get first record in current page
    **/
    private function getFirstItemInPage(){
        if($this->record_total > 0){
            $o = range(1,$this->record_total,$this->record_per_page);
            return isset($o[$this->current_page-1]) ? $o[$this->current_page-1] : 1; 
        }
        return false;
    }
}